<template>
  <body style="padding-top: 100px">
  <div>
    <h1>欢迎来到萌趣商城</h1>
    <div class="grid-content" style="margin-top:50px;margin-bottom: 10px">
      <img src="../../public/logo.png" height="92.53" width="100"/>
    </div>
    <el-card style="width: 600px;height: 365px;
    background-color: rgba(255,255,255,0.3);margin: 0 auto">
      <el-form :model="user" :rules="rules" ref="user" label-width="120px" style="width: 400px;margin: 50px auto;">
        <el-form-item label="用户名">
          <el-input type="text" v-model="user.username" placeholder="用户名长度必须在 4 到 15 个字符"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input type="text" v-model="user.nickname" placeholder="昵称长度必须在 4 到 15 个字符"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="user.password" placeholder="密码长度必须在 4 到 15 个字符"></el-input>
        </el-form-item>
        <el-form-item label="请再次输入密码">
          <el-input type="password" v-model="user.repassword" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="position: relative;left: -30px;border: 1px solid slategrey;background-color: hotpink" type="primary"@click="reg()">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <br>
    <p><a href="/sys-admin/Login">已有萌趣账户？点击此处跳转登录页面</a> </p>
  </div>
  </body>
</template>

<style>
body{
  background-image: url("../../public/bg.jpg");
  background-size: cover;/*把背景图片设置为封面*/
  background-repeat: no-repeat;
  text-align: center;
}

h1{
  font-size: 72px;
  color: hotpink;
  margin-bottom: 0;
}
img{width: 100px}
h2{
  font-size: 32px;
  color: #0096dc;
  margin-bottom: 0;
}

</style>

<script>

export default {
  data: function () {
    return {
      user: {
        username: "",
        password: "",
        repassword: "",
        nickname: ""
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '用户名长度必须在 4 到 15 个字符', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {min: 4, max: 15, message: '昵称长度必须在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '密码长度必须在 4 到 15 个字符', trigger: 'blur'}
        ],
        repassword: [
          {required: true, message: '请再次输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '密码长度必须在 4 到 15 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    reg() {
      //发出注册请求
      axios.post("/reg", user).then(function (response) {
        if (response.data == 1) {
          alert("注册成功")
          location.href = "/";
        } else {
          alert("用户已存在")
        }
      })
    }
  }
}
</script>